
package main

import (
	"fmt"
	"strings"
	"bytes"
)

func comma(s string) string {
	var pre = ""
	if (strings.HasPrefix(s, "+") || strings.HasPrefix(s, "-")) {
		pre = s[0:1]
		s = s[1:]
	}
	n := len(s)
	if n <= 3 {
		return pre + s
	}
	if (strings.Contains(s, ".")) { // 判断是否为小数
		inx := strings.Index(s, ".")
		st := s[inx+1:];
		var buf bytes.Buffer
		if (len(st) > 3) {
			n := []byte(st)
			for i, r := range n {
				buf.WriteByte(r)
				if (i != 0 && (i+1) % 3 == 0) {
					buf.WriteString(",")
				}
			}
		}
		return pre + comma(s[:inx]) + "." + buf.String()
	}
	
	return pre + comma(s[:n-3]) + "," + s[n-3:]
}

// 完善comma函数，以支持浮点数处理和一个可选的正负号的处理
func main() {
	fmt.Println(comma("15"))
	fmt.Println(comma("15442323"))
	fmt.Println(comma("15442323.2123"))
	fmt.Println(comma("+15442323.2123"))
	fmt.Println(comma("-15"))
	fmt.Println(comma("-15.4523575"))
	fmt.Println(comma("-15442323"))

}